package weaver.interfaces.zwgk.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.utils.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import weaver.general.BaseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-09-23
 * @Description: webservice 接口工具类
 * @ModifyLog:
 **/

public class WebServiceUtil extends BaseBean {

    public static void main(String[] args) {
        CheckAxmt410_icdWork();
    }

    public static void CreateApmt420Work() {
        String method = "CreateApmt420Work";
        Map<String, String> masterMap = new HashMap<>();
        masterMap.put("pmk01", "A111-2209170002");
        masterMap.put("pmk04", "2022/09/172");
        masterMap.put("pmk09", "");
        masterMap.put("pmk02", "REG");
        masterMap.put("pmk20", "");
        masterMap.put("pmk41", "");
        masterMap.put("pmk10", "");
        masterMap.put("pmk12", "tiptop");
        masterMap.put("pmk13", "A06");
        masterMap.put("pmk21", "");
        masterMap.put("pmk22", "");

        List<Map<String, String>> detailMapList = new ArrayList<>();
        Map<String, String> map1 = new HashMap<>();
        map1.put("pml02", "1");
        map1.put("pmk04", "001");
        map1.put("pml041", "成品");
        map1.put("pml35", "2022/09/17");
        map1.put("pml07", "EA");
        map1.put("pml08", "EA");
        map1.put("pml20", "100.00");
        map1.put("pml83", "");
        map1.put("pml85", "");
        map1.put("pmlud01", "");
        detailMapList.add(map1);
        Map<String, String> map2 = new HashMap<>();
        map2.put("pml02", "2");
        map2.put("pmk04", "002");
        map2.put("pml041", "封装");
        map2.put("pml35", "2022/09/17");
        map2.put("pml07", "EA");
        map2.put("pml08", "EA");
        map2.put("pml20", "200.00");
        map2.put("pml83", "");
        detailMapList.add(map2);
        JSONObject message = Apmt420Work(method, masterMap, detailMapList);
        System.out.println(message);
    }

    public static void CheckApmt420Work() {
        String method = "CheckApmt420Work";
        Map<String, String> masterMap = new HashMap<>();
        masterMap.put("pmk01", "A111-2209170002");
        masterMap.put("pmk02", "REG");
        masterMap.put("pmk04", "2022/09/172");
        masterMap.put("pmk09", "");
        masterMap.put("pmk12", "tiptop");
        masterMap.put("pmk13", "A06");
        masterMap.put("pmk20", "");
        masterMap.put("pmk21", "");
        masterMap.put("pmk22", "");
        masterMap.put("pmkud02", "kehuminc");

        List<Map<String, String>> detailMapList = new ArrayList<>();
        Map<String, String> map1 = new HashMap<>();
        map1.put("pml02", "1");
        map1.put("pmk04", "001");
        map1.put("pml041", "成品");
        map1.put("pml33", "2022/09/17");
        map1.put("pml34", "2022/09/17");
        map1.put("pml35", "2022/09/17");
        map1.put("pml07", "EA");
        map1.put("pml08", "EA");
        map1.put("pml20", "100.00");
        map1.put("pmlud01", "");
        map1.put("pmlud06", "NEW");
        detailMapList.add(map1);
        Map<String, String> map2 = new HashMap<>();
        map2.put("pml02", "2");
        map2.put("pmk04", "002");
        map2.put("pml041", "封装");
        map2.put("pml33", "2022/09/17");
        map2.put("pml34", "2022/09/17");
        map2.put("pml35", "2022/09/17");
        map2.put("pml07", "EA");
        map2.put("pml08", "EA");
        map2.put("pml20", "200.00");
        map2.put("pmlud01", "");
        map2.put("pmlud06", "OLD");
        detailMapList.add(map2);
        JSONObject message = Apmt420Work(method, masterMap, detailMapList);
        System.out.println(message);
    }

    public static void CheckAxmt410_icdWork() {
        String method = "CheckAxmt410_icdWork";
        Map<String, String> masterMap = new HashMap<>();
        masterMap.put("oea00", "1");
        masterMap.put("oea01", "S101-2302150001");
        masterMap.put("oea02", "22/02/15");
        masterMap.put("oea03", "01.002");
        masterMap.put("oea04", "01.002");
        masterMap.put("oea05", "1");
        masterMap.put("oea08", "1");
        masterMap.put("oea10", "PO1806110001");
        masterMap.put("oea1008", "2000");
        masterMap.put("oea14", "E01001");
        masterMap.put("oea15", "A01");
        masterMap.put("oea21", "SN00");
        masterMap.put("oea23", "USD");
        masterMap.put("oea31", "S001");
        masterMap.put("oea32", "NET45");
        masterMap.put("oea61", "2000");
        masterMap.put("oeaud07", "客户张三");
        masterMap.put("oea11", "9");
        masterMap.put("oea12", "OA010000");

        List<Map<String, String>> detailMapList = new ArrayList<>();
        Map<String, String> map1 = new HashMap<>();
        map1.put("pml02", "1");
        map1.put("oeb03", "1");
        map1.put("oeb04", "30.OB6220RVPA_P1G11-C");
        map1.put("oeb05", "EA");
        map1.put("oeb06", "OB6220RVPA-C");
        map1.put("oeb12", "10");
        map1.put("oeb13", "100");
        map1.put("oeb14", "1000");
        map1.put("oeb14t", "1000");
        map1.put("oeb15", "22/09/07");
        map1.put("oeb16", "22/09/07");
        detailMapList.add(map1);
        Map<String, String> map2 = new HashMap<>();
        map2.put("pml02", "2");
        map2.put("oeb03", "B");
        map2.put("oeb04", "30.OB6220RVPA_P1G11-B");
        map2.put("oeb05", "EA");
        map2.put("oeb06", "OB6220RVPA-B");
        map2.put("oeb12", "10");
        map2.put("oeb13", "100");
        map2.put("oeb14", "1000");
        map2.put("oeb14t", "1000");
        map2.put("oeb15", "22/09/07");
        map2.put("oeb16", "22/09/07");
        detailMapList.add(map2);
        JSONObject message = Axmt410_icdWork(method, masterMap, detailMapList);
        System.out.println(message);
    }

    public static void CreateAxmt410_icdWork() {
        String method = "CreateAxmt410_icdWork";
        Map<String, String> masterMap = new HashMap<>();
        masterMap.put("oea00", "1");
        masterMap.put("oea01", "S101-2302150001");
        masterMap.put("oea02", "22/02/15");
        masterMap.put("oea03", "01.002");
        masterMap.put("oea04", "01.002");
        masterMap.put("oea05", "1");
        masterMap.put("oea08", "1");
        masterMap.put("oea10", "PO1806110001");
        masterMap.put("oea1008", "2000");
        masterMap.put("oea14", "E01001");
        masterMap.put("oea15", "A01");
        masterMap.put("oea21", "SN00");
        masterMap.put("oea23", "USD");
        masterMap.put("oea31", "S001");
        masterMap.put("oea32", "NET45");
        masterMap.put("oea61", "2000");
        masterMap.put("oeaud07", "客户张三");
        masterMap.put("oea11", "9");
        masterMap.put("oea12", "OA010000");

        List<Map<String, String>> detailMapList = new ArrayList<>();
        Map<String, String> map1 = new HashMap<>();
        //map1.put("pml02", "1");
        map1.put("oeb03", "1");
        map1.put("oeb04", "30.OB6220RVPA_P1G11-C");
        map1.put("oeb05", "EA");
        map1.put("oeb06", "OB6220RVPA-C");
        map1.put("oeb12", "10");
        map1.put("oeb13", "100");
        map1.put("oeb14", "1000");
        map1.put("oeb14t", "1000");
        map1.put("oeb15", "22/09/07");
        map1.put("oeb16", "22/09/07");
        map1.put("oeb11", "");
        detailMapList.add(map1);
        Map<String, String> map2 = new HashMap<>();
        //map2.put("pml02", "2");
        map2.put("oeb03", "2");
        map2.put("oeb04", "30.OB6220RVPA_P1G11-B");
        map2.put("oeb05", "EA");
        map2.put("oeb06", "OB6220RVPA-B");
        map2.put("oeb12", "10");
        map2.put("oeb13", "100");
        map2.put("oeb14", "1000");
        map2.put("oeb14t", "1000");
        map2.put("oeb15", "22/09/07");
        map2.put("oeb16", "22/09/07");
        map1.put("oeb11", "");
        detailMapList.add(map2);
        JSONObject message = Axmt410_icdWork(method, masterMap, detailMapList);
        System.out.println(message);
    }

    /**
     * @Description: 2.2 请购单创建/核检接口
     * @Param: [paramJson]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/6/20
     */
    public static JSONObject Apmt420Work(String method, Map<String, String> masterMap, List<Map<String, String>> detailMapList) {
        JSONObject returnJson = new JSONObject();
        StringBuilder masterRecord = new StringBuilder("<Record>");
        for (Map.Entry<String, String> entry : masterMap.entrySet()) {
           masterRecord.append("<Field name='" + entry.getKey() + "' value='" + entry.getValue() + "'/>");
        }
        masterRecord.append("</Record>");
        String master = "<Master name='pmk_file'>" + masterRecord + "</Master>";

        StringBuilder details = new StringBuilder("<Detail name='pml_file'>");
        for (Map<String, String> stringMap : detailMapList) {
            StringBuilder detailRecord = new StringBuilder("<Record>");
            for (Map.Entry<String, String> entry : stringMap.entrySet()) {
                detailRecord.append("<Field name='" + entry.getKey() + "' value='" + entry.getValue() + "'/>");
            }
            detailRecord.append("</Record>");
            details.append(detailRecord);
        }
        details.append("</Detail>");
        String params = "<Document><RecordSet id='1'>" + master + details + "</RecordSet></Document>";
        try {
            String message = InvokeHandle(method, params);
            Document xmlDocument = DocumentHelper.parseText(message);
            if (xmlDocument != null) {
                Element rootElement = xmlDocument.getRootElement();
                Element Status = rootElement
                        .element("Body")
                        .element(method + "Response")
                        .element("response")
                        .element("Response")
                        .element("Execution").element("Status");
                String code = Status.attributeValue("code");
                String description = Status.attributeValue("description");
                returnJson.put("code", code);
                returnJson.put("description", description);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnJson;
    }

    /**
     * @Description: 2.3 销售订单创建/核检接口
     * @Param: [paramJson]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/6/20
     */
    public static JSONObject Axmt410_icdWork(String method, Map<String, String> masterMap, List<Map<String, String>> detailMapList) {
        JSONObject returnJson = new JSONObject();
        StringBuilder masterRecord = new StringBuilder("<Record>");
        for (Map.Entry<String, String> entry : masterMap.entrySet()) {
           masterRecord.append("<Field name='" + entry.getKey() + "' value='" + entry.getValue() + "'/>");
        }
        masterRecord.append("</Record>");
        String master = "<Master name='oea_file'>" + masterRecord + "</Master>";

        StringBuilder details = new StringBuilder("<Detail name='oeb_file'>");
        for (Map<String, String> stringMap : detailMapList) {
            StringBuilder detailRecord = new StringBuilder("<Record>");
            for (Map.Entry<String, String> entry : stringMap.entrySet()) {
                detailRecord.append("<Field name='" + entry.getKey() + "' value='" + entry.getValue() + "'/>");
            }
            detailRecord.append("</Record>");
            details.append(detailRecord);
        }
        details.append("</Detail>");
        String params = "<Document><RecordSet id='1'>" + master + details + "</RecordSet></Document>";
        try {
            String message = InvokeHandle(method, params);
            Document xmlDocument = DocumentHelper.parseText(message);
            if (xmlDocument != null) {
                Element rootElement = xmlDocument.getRootElement();
                System.out.println(rootElement.asXML());
                Element Status = rootElement
                        .element("Body")
                        .element(method + "Response")
                        .element("response")
                        .element("Response")
                        .element("Execution").element("Status");
                String code = Status.attributeValue("code");
                String description = Status.attributeValue("description");
                returnJson.put("code", code);
                returnJson.put("description", description);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnJson;
    }

    /**
     * @Description: 通用调用接口
     * @Param: [paramJson]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/6/20
     */
    public static String InvokeHandle(String method, String bodyXML) {
        String wsdl = "http://10.0.3.51/web/ws/r/aws_ttsrv2_toptest";
        String Header_beg = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"" +
                " xmlns:tip=\"http://www.dsc.com.tw/tiptop/TIPTOPServiceGateWay\">\n<soapenv:Header/>\n";
        String Header_end = "</soapenv:Envelope>\n";
        String Body_beg = "<soapenv:Body>\n";
        String Body_end = "</soapenv:Body>\n";
        String Method_beg = "<tip:" + method + "Request>\n";
        String Method_end = "</tip:" + method + "Request>\n";
        String request_beg = "<tip:request><![CDATA[<Request>\n";
        String request_end = "</Request>]]></tip:request>\n";
        String Access = "<Access>\n" +
                "        <Authentication user=\"apiadmin\" password=\"apiadmin\"/>\n" +
                "        <Connection application=\"OA\" source=\"127.0.0.1\"/>\n" +
                "        <Organization name=\"OBDEMO\"/>\n" +
                "        <Locale language=\"zh_cn\"/>\n" +
                "    </Access>\n";
        String Document = "<RequestContent>\n" +
                "        <Parameter/>" + bodyXML + "\n</RequestContent>";

        StringBuilder templateXML = new StringBuilder(Header_beg);
        templateXML.append(Body_beg);
        templateXML.append(Method_beg);
        templateXML.append(request_beg);
        templateXML.append(Access);
        templateXML.append(Document);
        templateXML.append(request_end);
        templateXML.append(Method_end);
        templateXML.append(Body_end);
        templateXML.append(Header_end);
        String xml = templateXML.toString();
        System.out.println(xml);
        String message = doPostWebServiceURL(wsdl, xml, "\"\"");
        return message;
    }

    /**
     * @param url
     * @param xmlParams
     * @return
     * @throws Exception
     */
    public static String doPostWebServiceURL(String url, String xmlParams, String soapAction) {
        String result = "";
        // 创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpPost httpPost = new HttpPost(url);
        try {
            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
            httpPost.setHeader("SOAPAction", soapAction);
            StringEntity data = new StringEntity(xmlParams, "UTF-8");
            httpPost.setEntity(data);
            CloseableHttpResponse response = closeableHttpClient
                    .execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                result = EntityUtils.toString(httpEntity, "UTF-8");
                result = result.replaceAll("&lt;", "<").replaceAll("&gt;", ">");
            }
        } catch (Exception e) {
            System.out.println("调用远程WebService接口异常:{}" + e);
        } finally {
            IOUtils.closeQuietly(closeableHttpClient);
        }
        return result;
    }

}
